package com.tensquare.article.controller;

import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommentService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 使用Redis记录一个评论一个用户只能点赞一次
     * @param commentId
     * @param comment
     * @return
     */
    @RequestMapping(value="/thumbup/{commentId}",method = RequestMethod.PUT)
    public Result thumbup(@PathVariable String commentId,@RequestBody Comment comment){
        //防止重复点赞
        //从Redis中查询是否存在，如果为空，则可以点赞否则不能点赞
        String userId = "hujian";
        String value = redisTemplate.boundValueOps("thumbup" + "_" + userId + "_" + commentId).get();
        if(StringUtils.isEmpty(value)){
            //可以点赞
            commentService.thumbup(commentId,comment);
            //将点赞记录保存在Redis中
            redisTemplate.boundValueOps("thumbup" + "_" + userId + "_" + commentId).set("1");
            //返回结果信息
            return new Result(true,StatusCode.OK,"点赞成功");
        }
        return new Result(true,StatusCode.REPERROR,"该用户对该评论已点过赞，不允许重复点赞");
    }

    /**
     * 新增
     * @param comment
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result save(@RequestBody Comment comment){
        commentService.save(comment);
        return new Result(true,StatusCode.OK,"新增成功");
    }

    /**
     * 删除
     * @param commentId
     * @return
     */
    @RequestMapping(value = "/{commentId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable String commentId){
        commentService.deleteById(commentId);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Comment> comments = commentService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",comments);
    }

    @RequestMapping(value="/{commentId}",method = RequestMethod.PUT)
    public Result updateById(@PathVariable String commentId,@RequestBody Comment comment){
        boolean result = commentService.updateById(commentId,comment);
        if(result){
            return new Result(true, StatusCode.OK,"修改成功");
        }else{
            return new Result(true, StatusCode.ERROR,"修改失败,ID不存在");
        }
    }


    /**
     * 根据ID查询
     * @param commentId
     * @return
     */
    @RequestMapping(value="/{commentId}",method = RequestMethod.GET)
    public Result findById(@PathVariable("commentId") String commentId){
        Comment comment = commentService.findById(commentId);
        return new Result(true,StatusCode.OK,"查询成功",comment);
    }
}
